package net.bithappens.hnqe.impl.mapper;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.bithappens.hnqe.mapper.IMapper;

public class MapperContainer {

	private Map<String, IMapper> mappersByMapperId = new HashMap<String, IMapper>();
	
	private Map<String, Map<String, IMapper>> mappersByInstanceId = new HashMap<String, Map<String, IMapper>>();
	
	
	public void addMapper(IMapper mapper){
		
		mappersByMapperId.put(mapper.getMapperId(), mapper);
		
		for(String instanceId : mapper.getInstanceIds()){
			
			if(mappersByInstanceId.containsKey(instanceId)){
				mappersByInstanceId.get(instanceId).put(mapper.getMapperId(), mapper);
			} else {
				Map<String, IMapper> instanceCache = new HashMap<String, IMapper>();
				instanceCache.put(mapper.getMapperId(), mapper);
				mappersByInstanceId.put(instanceId, instanceCache);
			}
			
		}
		
	}

	public boolean containsMapper(IMapper mapper) {

		return mappersByMapperId.containsKey(mapper.getMapperId());
		
	}


	public Collection<IMapper> getMappersOfInstance(String instanceId) {

		List<IMapper> toReturn = new LinkedList<IMapper>();
		
		if(mappersByInstanceId.containsKey(instanceId)){
			toReturn.addAll(mappersByInstanceId.get(instanceId).values());
		}

		return toReturn;
	}

	public Collection<IMapper> getAllMappers() {
		
		List<IMapper> toReturn = new LinkedList<IMapper>();
		
		toReturn.addAll(mappersByMapperId.values());
		
		return toReturn;
	}
}
